package com.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.HashMap;
import java.util.Map;

public class HttpRequest implements Request {
	private Socket socket;
	private String read;
	private Map<String, String> map = new HashMap<>();

	public HttpRequest(Socket socket) {
		this.socket = socket;
	}

	public void init() {
		try {
			InputStream inputStream = socket.getInputStream();
			BufferedReader bs = new BufferedReader(new InputStreamReader(
					inputStream));
			read = bs.readLine();
			String readLine = null;
			while ((readLine = bs.readLine()) != null && readLine.length() > 0) {
				int indexOf = readLine.indexOf(":");
				if (indexOf != -1) {
					String string = readLine.substring(0, indexOf);
					String substring = readLine.substring(indexOf + 2);
					map.put(string, substring);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Override
	public Map<String, String> getHeaders() {
		return map;
	}

	@Override
	public String getHeader(String name) {
		return map.get(name);
	}

	@Override
	public String getMethod() {
		if (read == null) {
			return "";
		}
		int indexOf = read.indexOf(" ");
		if (indexOf < 0) {
			return "";
		}
		return read.substring(0, indexOf);
	}

	@Override
	public String getQueryString() {
		String url = getRequestURL();
		int indexOf = url.indexOf("?");
		if (indexOf < 0) {
			return "";
		}
		return url.substring(indexOf + 1);
	}

	@Override
	public String getRequestURL() {
		if (read==null) {
			return "";
		}
		return read.substring(read.indexOf(" ") + 1,
				read.indexOf(" HTTP"));
	}

	@Override
	public String getParameter(String name) {
		if (read == null) {
			getHeaders();
		}

		String query = getQueryString();
		String[] split = query.split("&");
		for (String string : split) {
			int indexOf = string.indexOf("=");
			String substring = string.substring(0, indexOf);
			String substring2 = string.substring(indexOf + 1);
			map.put(substring, substring2);
		}
		return map.get(name);
	}

	@Override
	public String getRemoteAddress() {
		SocketAddress remoteSocketAddress = socket.getRemoteSocketAddress();
		return remoteSocketAddress.toString().substring(1,
				remoteSocketAddress.toString().indexOf(":"));
	}

	@Override
	public String getRemotePort() {
		SocketAddress remoteSocketAddress = socket.getRemoteSocketAddress();

		return remoteSocketAddress.toString().substring(
				remoteSocketAddress.toString().indexOf(":") + 1);
	}

	@Override
	public InputStream getInputStream() {
		try {
			InputStream inputStream = socket.getInputStream();
			return inputStream;
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	public String getContenxtPath() {
		return null;
	}

	@Override
	public String getURL() {
		if (read == null) {
			init();
		}
		String url = getRequestURL();
		int indexOf = url.indexOf("?");
		if (indexOf<0) {
			int lastIndexOf = url.lastIndexOf("/");
			if (lastIndexOf<0) {
				return "";
			}
			return url.substring(lastIndexOf+1);
		}
		int lastIndexOf = url.lastIndexOf("/");
		if (lastIndexOf<0) {
			return "";
		}
		return url.substring(lastIndexOf+1, indexOf);
		
	}

}
